package com.dragon.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dragon.manage.bean.AmusementInfo;
import com.dragon.manage.bean.AmusementType;
import com.dragon.manage.bean.GameName;
import com.dragon.manage.bean.ManageUser;
import com.dragon.manage.bean.MatchInfo;
import com.dragon.manage.bean.TeamInfo;
import com.dragon.manage.dao.IAmusementInfoDAO;
import com.dragon.manage.dao.IAmusementTypeDAO;
import com.dragon.manage.dao.IGameNameDAO;
import com.dragon.manage.dao.IMatchInfoDAO;
import com.dragon.manage.dao.ITeamInfoDAO;
import com.dragon.manage.service.IManageAmusementSerice;
import com.dragon.manage.util.ManageUtil;
import com.dragon.manage.util.StaticData;

public class ManageAmusementServiceImpl implements IManageAmusementSerice{
	
	/** 竞猜信息 */
	private IAmusementInfoDAO amusementInfoDAO;
	/** 战队信息*/
	private ITeamInfoDAO teamInfoDAO;
	/** 比赛信息**/
	private IMatchInfoDAO matchInfoDAO;
	/**
	 * 开盘类型
	 */
	private IAmusementTypeDAO amusementTypeDAO;
	/**
	 * 游戏类型
	 */
	private IGameNameDAO gameNameDAO;
	


	@Override
	public List<TeamInfo> getTeamList(int type) throws Exception{
		List<TeamInfo> teamList = new ArrayList<TeamInfo>();
		teamList = teamInfoDAO.getTeamInfoList(type);
		return teamList;
	}

	@Override
	public boolean createMatch(MatchInfo matchInfo) throws Exception {
		boolean b = false;
		int reInt = matchInfoDAO.createMatchInfo(matchInfo);
		if(reInt>0){
			b = true;
		}
		return b;
	}
	
	@Override
	public List<AmusementType> getAmusementTypeList() throws Exception {
		List<AmusementType> amusementTypeList = amusementTypeDAO.getAmusementTypeList();
		return amusementTypeList;
	}
	
	@Override
	public TeamInfo getTeamInfoById(int id) throws Exception {
		TeamInfo teamInfo = teamInfoDAO.getTeamInfoByID(id);
		return teamInfo;
	}
	
	@Override
	public boolean createAmusement(AmusementInfo amusementInfo) throws Exception {
		amusementInfoDAO.createAmusementInfo(amusementInfo);
		return false;
	}
	
	@Override
	public List<GameName> getGameNameList() throws Exception {
		return gameNameDAO.getGameNameList();
	}
	
	@Override
	public JSONObject getMatchInfoListByType(int type,int page) throws Exception {
		JSONObject reJs = new JSONObject(); 
		List<MatchInfo> list = matchInfoDAO.getMatchInfoListByType(type,page,
				StaticData.AmusementStatus.putaway.getType(),type);
		JSONArray jsList = new JSONArray();
		for(MatchInfo tmp:list){
			jsList.add(ManageUtil.getMatchPage(tmp, teamInfoDAO));
		}
		reJs.put("matchInfoList", jsList);
		reJs.put(StaticData.reKey.total.getKey(), 
				matchInfoDAO.getMatchCountByType(type, StaticData.AmusementStatus.putaway.getType()));
		return reJs;
	}
	
	@Override
	public MatchInfo getMatchInfoById(int id) throws Exception {
		return matchInfoDAO.getMatchInfoByID(id);
	}
	
	@Override
	public AmusementInfo getAmusementInfoById(int id) throws Exception {
		return amusementInfoDAO.getAmusementInfoByID(id);
	}
	@Override
	public JSONObject updateMatchInfo(MatchInfo matchInfo,ManageUser manageUser) throws Exception {
		JSONObject reJson = new JSONObject();
		/*
		 * 如果是更新比赛结果的，
		 * 则判断是否已经更新完了，
		 * 更新完了则更新比赛的状态流转到下一级
		 */
		if(matchInfo.getMatchStatus()==StaticData.AmusementStatus.first_result_check.getType()){
			List<AmusementInfo> list = 
					amusementInfoDAO.getAmusementInfoListByMatchStatus(matchInfo.getId(),
					StaticData.AmusementStatus.putaway.getType());
			if(list==null||list.size()==0){
				MatchInfo tmp = matchInfoDAO.getMatchInfoByID(matchInfo.getId());
				tmp.setMatchStatus(StaticData.AmusementStatus.first_result_check.getType());
				tmp.setFirstResult(manageUser.getId());
				 matchInfoDAO.updateMatchInfoDB(tmp);
				 reJson.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
			}else{
				reJson.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_affirm_finish.getType());
			}
		}else{
			matchInfoDAO.updateMatchInfoDB(matchInfo);
			reJson.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		}
		return reJson;
	}
	
	
	@Override
	public void updateAmusementInfo(AmusementInfo amusementInfo)
			throws Exception {
		amusementInfoDAO.updateAmusementInfoDB(amusementInfo);
		
	}
	
	
	@Override
	public JSONObject deleteAmusementInfo(int id) throws Exception {
		JSONObject reJson = new JSONObject();
		AmusementInfo amusementInfo = amusementInfoDAO.getAmusementInfoByID(id);
		if(amusementInfo!=null&&amusementInfo.getCount()>0){
			reJson.put(StaticData.reKey.code.getKey(), StaticData.codeType.amusementInfo_IS_BET.getType());
			return reJson;
		}
		amusementInfoDAO.deleteAmusementInfoByID(id);
		reJson.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJson;
	}
	
	@Override
	public JSONObject deleteMatch(int matchId) throws Exception {
		JSONObject reJson = new JSONObject();
		List<AmusementInfo> list = amusementInfoDAO.getAmusementInfoListByMatchId(matchId);
		if(list!=null&&list.size()>0){
			reJson.put(StaticData.reKey.code.getKey(), StaticData.codeType.amusementInfo_IS_BET.getType());
			return reJson;
		}
		matchInfoDAO.deleteMatchInfoByID(matchId);
		reJson.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		return reJson;
	}
	@Override
	public JSONArray getAmusementInfoList(int matchId) throws Exception {
		List<AmusementInfo> list = amusementInfoDAO.getAmusementInfoListByMatchId(matchId);
		JSONArray reList = new JSONArray();
		for(AmusementInfo tmp:list){
			reList.add(ManageUtil.getAmusementInfoPage(tmp, amusementTypeDAO, matchInfoDAO, teamInfoDAO));
			
		}
		return reList;
	}
	
	@Override
	public AmusementType getAmusementTypeById(int id) throws Exception {
		return amusementTypeDAO.getAmusementTypeByID(id);
	}

	public IAmusementInfoDAO getAmusementInfoDAO() {
		return amusementInfoDAO;
	}

	public void setAmusementInfoDAO(IAmusementInfoDAO amusementInfoDAO) {
		this.amusementInfoDAO = amusementInfoDAO;
	}

	public ITeamInfoDAO getTeamInfoDAO() {
		return teamInfoDAO;
	}

	public void setTeamInfoDAO(ITeamInfoDAO teamInfoDAO) {
		this.teamInfoDAO = teamInfoDAO;
	}

	public IMatchInfoDAO getMatchInfoDAO() {
		return matchInfoDAO;
	}

	public void setMatchInfoDAO(IMatchInfoDAO matchInfoDAO) {
		this.matchInfoDAO = matchInfoDAO;
	}

	public IAmusementTypeDAO getAmusementTypeDAO() {
		return amusementTypeDAO;
	}

	public void setAmusementTypeDAO(IAmusementTypeDAO amusementTypeDAO) {
		this.amusementTypeDAO = amusementTypeDAO;
	}

	public IGameNameDAO getGameNameDAO() {
		return gameNameDAO;
	}

	public void setGameNameDAO(IGameNameDAO gameNameDAO) {
		this.gameNameDAO = gameNameDAO;
	}

}
